package com.o2o.cleaning.month.platform.ebusiness_plat.dazhongdp

import com.alibaba.fastjson.{JSON, JSONObject}
import org.apache.spark.sql.SparkSession
import org.elasticsearch.spark._

object DZDP_es247 {

  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder()
      .appName("SparkTest")
//      .master("local[*]")
      .config("es.nodes", "192.168.1.29")
      .config("es.port", "9200")
      .config("es.net.http.auth.user", "elastic")
      .config("es.net.http.auth.pass", "changeme")
      .config("es.batch.write.retry.count", "10")
      .config("es.batch.write.retry.wait", "60")
      .config("cluster.name", "O2OElastic")
      .getOrCreate()
    val sc = spark.sparkContext
    val sqlcontext = spark.sqlContext

    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")

    /** *
      * 需要修改的参数信息
      */
    var year ="2020"
    var month = "8"
    var platform = "dzdp"
    var index247 = s"247_${year}_dazhongdp/dazhongdp_${year}_${month}"

    //good路径
    var goodPath = s"s3a://o2o-dataproces-group/zsc/${year}/${month}/${platform}/good/"

    //入库
    val df = spark.read.json(goodPath).toJSON.rdd.map(line =>{

      //flavors类型不一致
      val lines = JSON.parseObject(line)
      var f = lines.get("flavors").toString
      lines.put("flavors", f)

      //evaluates存在空值   lines
      if(lines.containsKey("evaluates") ){
        val evaluates = lines.get("evaluates").toString

        var ev = "-1"
        var str = new JSONObject
        if (evaluates.equals("-1")||evaluates.equals("")) {
          ev = "{\"fuyi\":\"-1\"}"
        } else {
          str = JSON.parseObject(evaluates)
        }
        if (!ev.contains("fuyi")) {
          lines.put("evaluates", str)
        } else {
          val evs = JSON.parseObject(ev)
          lines.put("evaluates", evs)
        }
      }

      lines.remove("shop_pre_md5")
      lines.remove("md5_id")
      lines.remove("pre_md5")
      lines.remove("platformName_spelling")
      lines.remove("md5_shopId")
      lines.remove("type_xichen")
      lines.remove("gehu_type")
      lines.remove("promotion_info")
//      lines.remove("is_pingou")
      lines
    })
      .saveToEs(index247, Map("es.mapping.id" -> "good_id"))
  }

}
